package com.umpay.utils.mail;

import java.io.File;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.umpay.utils.PropertyUtil;
import com.umpay.utils.SpringUtil;

public class SendMailUtil {
	
	public static final Logger log = LogManager.getLogger(SendMailUtil.class);
	/**
	 * 
	 * desc:简单的邮件发送
	 * @param mailto
	 * @param subject
	 * @param mailContent
	 */
	public static void sendMail(String mailto,String subject,String mailContent){
		sendMail(mailto,"", subject, mailContent, null);
	}
	public static void sendMail(String mailto,String subject,String mailContent,String smtpserver,String smtpport,String smtpuser,String smtppwd,String mailfrom){
		sendMail(mailto,"", subject, mailContent, null,smtpserver,smtpport,smtpuser,smtppwd,mailfrom);
	}
	/**
	 * 
	 * desc:邮件发送(带附件)
	 * <p>创建人：liuguangjun , Mar 5, 2014 7:08:04 PM</p>
	 * @param mailto
	 * @param subject
	 * @param mailContent
	 * @param fileList
	 */
	public static void sendMail(String mailto,String subject,String mailContent,List<File> fileList){
		sendMail(mailto,"", subject, mailContent, fileList);
	}
	
	public static void sendMail(String mailto,String mailcc,String subject, String mailContent, List<File> fileList,
			String smtpserver,String smtpport,String smtpuser,String smtppwd,String mailfrom){
		log.debug("=============发送邮件通知==============");
		try {
			MailSender sender = new SMTP_Sender();
			sender.setServerHost(smtpserver);
			sender.setServerPort(Integer.parseInt(smtpport)); //端口
			sender.setAuthen(true); 
			sender.setUserName(smtpuser);
			sender.setUserPwd(smtppwd);
			sender.setFrom(mailfrom); //发送人
			sender.setTo(mailto);   //发送到人员
			sender.setCc(mailcc);  //抄送人
			sender.setSubject(subject);
			sender.setTxtContent(mailContent); //邮件内容
			log.info(String.format("发送邮件smtpserver:%s,smtpport:%s,smtpuser:%s,smtppwd:%s,mailfrom:%s,mailto:%s,mailcc:%s",
					smtpserver,smtpport,smtpuser,smtppwd,mailfrom,mailto,mailcc));
			if(fileList!= null&&fileList.size()>0){  //是否发送附件
				sender.setAttachFile(fileList);  //增加附件
			}
			sender.sendMail();   //发送邮件
		} catch (Exception e) {
			log.error("发送邮件异常：" + e.getMessage(), e);
		}
	}
	
	/**
	 * 
	 * desc:邮件发送(带附件)
	 * @param mailto
	 * @param mailcc
	 * @param subject
	 * @param mailContent
	 * @param fileList
	 */
	public static void sendMail(String mailto,String mailcc,String subject, String mailContent, List<File> fileList){
		PropertyUtil propertyUtil = (PropertyUtil)SpringUtil.getBean("propertyUtil");
		String smtpserver = propertyUtil.getProperty(("mail.smtp.host")); //邮箱服务器设置
		String smtpport = propertyUtil.getProperty(("mail.smtp.port"));//邮件服务器默认端口
		String smtpuser = propertyUtil.getProperty(("mail.username"));//邮件服务器登陆用户名
		String smtppwd = propertyUtil.getProperty(("mail.password"));//邮件服务器登陆密码
		String mailfrom = propertyUtil.getProperty(("mail.from"));//发件人
		sendMail(mailto, mailcc, subject, mailContent, fileList, smtpserver, smtpport, smtpuser, smtppwd, mailfrom);
	}
}
